package com.yoomiito.app.ui.fragment.my;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.adapter.my.MyInviteAdapter;
import com.yoomiito.app.model.my.MyInviteBean;
import com.yoomiito.app.present.my.fragment.MyInviteFmPresenter;
import com.yoomiito.app.ui.my.MyInviteActivity;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.widget.SpacingDecoration;

import cn.droidlover.xdroidmvp.net.ApiException;

/**
 * 我的邀请fm
 */
public class MyInviteFragment extends MyOrderBaseFragment<MyInviteFmPresenter> {
    //    @BindView(R.id.rv_order)
//    RecyclerView mRecyclerView;
    private MyInviteAdapter mAdapter;
    private int type;
    private String time = "";
    private String name = "";

    @Override
    public MyInviteFmPresenter newP() {
        return new MyInviteFmPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt(Constant.KEY_TYPE, Constant.NAMAL_TYPE_TAOBAO);
        }
        mAdapter = new MyInviteAdapter(null, type);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        mRecyclerView.addItemDecoration(new SpacingDecoration(0, DensityUtil.dp2px(10), false));
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            MyInviteBean.DataBean dataBean = mAdapter.getData().get(position);
            switch (view.getId()) {
                case R.id.userFlagIv:
                    showTip(view,
                            dataBean.getReduce() == 1 ? "有效创粉：在你升级创粉之后升级的创粉用户" : " 紧缩创粉：在你升级创粉之前升级的创粉用户，已被紧缩",
                            2);
                    break;
                case R.id.copy:
                    AllUtils.copyText(context, dataBean.getMobile());
                    YMToast.showText("复制成功");
                    break;
            }

        });
        getNewData();

    }

    @Override
    public void handleError(ApiException e) {

    }

    public void setInviteData(MyInviteBean data) {
        if (currentPage == 1) {
            ((MyInviteActivity) context).setTotal(
                    "" + data.totalAll);
            ((MyInviteActivity) context).setTabNum(
                    data.member + "",
                    data.getVipInviteNum() + "",
                    data.getSddInviteNum() + "");
            if (data == null || data.getData() == null || data.getData().size() == 0) {
                mStatusManager.showEmpty();
            } else {
                mAdapter.setNewData(data.getData());
                mSmartRefreshLayout.finishRefresh(true);
                mStatusManager.showContent();
            }
        } else {
            mSmartRefreshLayout.finishLoadMore(true);
            if (data.getData() == null || data.getData().size() == 0) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                mAdapter.addData(data.getData());
            }
        }
    }

    public void finishRefresh() {
        mSmartRefreshLayout.finishRefresh(true);
        mSmartRefreshLayout.finishLoadMore(true);
    }

    @Override
    protected boolean useMyItemDecoration() {
        return true;
    }

    @Override
    protected void getNewData() {
        getP().getInviteData(currentPage, type, time, name);
    }

    public void setData(String name,String time){
        this.name = name;
        this.time = time;

    }

    @Override
    public void eventMessage(EventMessage eventMessage) {
        super.eventMessage(eventMessage);
        if (eventMessage.getEventType().equals("invite")) {
            time = eventMessage.getMsg();
            name = eventMessage.getGoodsID();
            getP().getInviteData(currentPage, type, time, name);
        }
    }

    @Override
    public String getEmptyContent() {
        return "未邀请好友";
    }
}
